package cn.hefan.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.hefan.domain.Admin;
import cn.hefan.domain.Pages;
import cn.hefan.domain.User;
import cn.hefan.util.DataSourceUtils;

public class findUserDao {

	public static int findCount() throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
	    String sql = "select * from user";
	   List<User> user = qr.query(sql,new BeanListHandler<User>(User.class));
		return user.size();
	}

	public static List<User> findUser(int start,int pageSize) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		 String sql = "select * from user limit ? , ?";
		 return qr.query(sql,new BeanListHandler<User>(User.class),start,pageSize);
	}
	
	public static int findDicount(String divide) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
	    String sql = "select * from user where divide=?";
	   List<User> user = qr.query(sql,new BeanListHandler<User>(User.class),divide);
		return user.size();
		
	}
	
	public static List<User> findDiUser(String divide,int start,int pageSize) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		 String sql = "select * from user where divide=? limit ? , ?";
		 return qr.query(sql,new BeanListHandler<User>(User.class),divide,start,pageSize);
	}

	public static List<User> finUname(String name) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql  = "select * from user where name=?";
		 return qr.query(sql,new BeanListHandler<User>(User.class),name);
	}

	public static int addUser(User user) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql  = "insert into user(name,username,password,idcard,divide) values(?,?,?,?,?)";
		return qr.update(sql,user.getName(),user.getUsername(),user.getPassword(),user.getIdcard(),user.getDivide());
	}

	public static void delectUser(String username) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "delete from user where username=?";
		qr.update(sql,username);
	}

}
